﻿using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Data;
using Representation=Asistente.Model.Representation;

namespace Asistente
{
    /// <summary>
    /// Interaction logic for MultipleEntitySelection.xaml
    /// </summary>
    public partial class MultipleEntitySelection
    {
        public readonly MultipleEntitySelectionViewModel ViewModel;

        public MultipleEntitySelection()
        {
            InitializeComponent();
            ObjectDataProvider provider = this.Resources["viewModel"] as ObjectDataProvider;
            this.ViewModel = provider != null ? provider.ObjectInstance as MultipleEntitySelectionViewModel : null;
        }

        void PopulateEntities(Representation.Application app)
        {
            if (this.ViewModel != null)
            {
                this.ViewModel.PopulateEntities(app);
            }
        }

        List<Representation.Entity> GetSelectedEntities()
        {
            var entities = new List<Representation.Entity>();
            entities.AddRange(this.ViewModel.Entities.Where(entity => entity.IsChecked));
            return entities;
        }

        public static List<Representation.Entity> ShowModal(Representation.Application app)
        {
            var dialog = new MultipleEntitySelection();
            dialog.PopulateEntities(app);
            if (dialog.ShowDialog() ?? true)
            {
                return dialog.GetSelectedEntities();    
            }
            return null;
        }

        private void ButtonOkClick(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
        }

        private void ButtonCancelClick(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
    }
}
